package com.easylinkin.linkappapi.common.translate.impl;

import cn.hutool.cache.Cache;
import cn.hutool.cache.CacheUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.easylinkin.linkappapi.common.translate.Code2Text;
import com.easylinkin.linkappapi.common.translate.CodeTranslateor;
import com.easylinkin.linkappapi.common.translate.CodeTranslateorFactory;
import com.easylinkin.linkappapi.common.translate.TypeEnum;
import com.easylinkin.linkappapi.roster.entity.RailLinkappRosterPersonnel;
import com.easylinkin.linkappapi.roster.mapper.LinkappRosterPersonnelMapper;
import com.google.common.cache.CacheBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

/**
 * 花名册翻译器
 */
@Component
public class RosterNameTranslateor implements CodeTranslateor {

    private static Cache<String, String> cache;

    public RosterNameTranslateor() {
        cache = CacheUtil.newLRUCache(100000);
    }

    @Autowired
    private LinkappRosterPersonnelMapper linkappRosterPersonnelMapper;

    @PostConstruct
    public void init() {
        CodeTranslateorFactory.register(this.getClass(), this);
        List<RailLinkappRosterPersonnel> railLinkappRosterPersonnels = linkappRosterPersonnelMapper.selectList(new QueryWrapper<>());
        if (CollectionUtil.isNotEmpty(railLinkappRosterPersonnels)) {
            for (RailLinkappRosterPersonnel railLinkappRosterPersonnel : railLinkappRosterPersonnels) {
                cache.put(railLinkappRosterPersonnel.getId().toString(), railLinkappRosterPersonnel.getRealName());
            }
        }
    }

    @Override
    public String translate(Object obj, Object value, Code2Text ann) {
        if (null == value) {
            return null;
        }
        if (value instanceof List) {
            List l = (List) value;
            List<String> list = new ArrayList<>(l.size());
            l.forEach((v) -> {
                String name = this.getName(v.toString());
                list.add(name);
            });
            return StrUtil.join("，", list);
        }
        if (ann.type() == TypeEnum.SPILT) {
            List<?> list = (List<?>) value;
            StringBuilder sb = new StringBuilder();
            for (Object id : list) {
                sb.append(this.getName(id.toString())).append("/");
            }
            return sb.toString();
        } else {
            return this.getName(value.toString());
        }
    }

    private String getName(String id) {
        String name = cache.get(id);
        if (StrUtil.isEmpty(name)) {
            RailLinkappRosterPersonnel railLinkappRosterPersonnel = linkappRosterPersonnelMapper.selectById(id);
            if (null != railLinkappRosterPersonnel) {
                name = railLinkappRosterPersonnel.getRealName();
                cache.put(id, name);
            }
        }
        return name;
    }


}
